package com.jwdf.uda.ui.adapter.viewholder;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.ItemQuestionBinding;
import com.jwdf.uda.ui.activity.QuizDetailActivity;
import com.jwdf.uda.ui.activity.UserInfoActivity;
import com.jwdf.uda.ui.adapter.AbsViewHolder;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.DisplayUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by fatsfan on 2016/9/9.
 */
public class IndexViewHolder extends AbsViewHolder<PbBean.PbBaseQuestion> {
    private Context context;
    private boolean hasAction;
    private ItemQuestionBinding mBinding;

    public IndexViewHolder(Context context, View convertView, boolean hasAction) {
        super(convertView);
        this.context = context;
        this.hasAction = hasAction;
        mBinding = DataBindingUtil.bind(convertView);
    }

    @SuppressLint("StringFormatMatches")
    @Override
    protected void onBindView(PbBean.PbBaseQuestion item) {
        mBinding.tvTitle.setText(item.getTitle());
        mBinding.tvCount.setText(convertView.getContext().getString(R.string.item_top_count, item.getPv(), item.getConcernCount(), item.getAnswerCount()));

        final PbBean.PbBaseQuestionAnswer bestAnswer = item.getBestAnswer();
        if(bestAnswer!=null){
        mBinding.tvAdmireNum.setText(String.valueOf(bestAnswer.getLikeCount()));
        if(bestAnswer.getLikeCount() >= 6)
            mBinding.ivReplyLevel.setVisibility(View.VISIBLE);

        final int quickImageCount = bestAnswer.getQuickImageCount();

        final FrameLayout layIcon = mBinding.layIcon;
        if(quickImageCount == 0) {
            layIcon.setVisibility(View.GONE);
        } else {
            layIcon.setVisibility(View.VISIBLE);
            mBinding.tvThumbNum.setText(String.valueOf(quickImageCount));

            final ImageView ivThumb = mBinding.ivThumb;
            final ViewGroup.LayoutParams lpThumb = ivThumb.getLayoutParams();
            lpThumb.width = lpThumb.height = DisplayUtils.getDeviceWidth(context) >> 2;
            if(bestAnswer.getQuickImageInfoList().size()>0) {
                ImageLoader.getInstance().displayImage(bestAnswer.getQuickImageInfoList().get(0).getUrl(), ivThumb);
            }
        }
        }


        final PbBean.PbUserSimple userInfo = item.getUserInfo();
        mBinding.tvUserName.setText(userInfo.getNickname());

        final ImageView ivHeader = mBinding.ivHeader;
        final ViewGroup.LayoutParams lpHeader = ivHeader.getLayoutParams();
        lpHeader.width = lpHeader.height = DisplayUtils.getDeviceWidth(context) / 10;

        ImageLoader.getInstance().displayImage(userInfo.getAvatar().getUrl(), ivHeader);

        mBinding.layTitle.setOnClickListener(this);
        mBinding.vUserInfo.setOnClickListener(this);
        mBinding.layAnswer.setOnClickListener(this);

        setAction();
    }

    private void setAction() {
        final RelativeLayout layAction = mBinding.layAction;
        final PbBean.PbBaseQuestionAnswer bestAnswer = item.getBestAnswer();
        final String quickDescription = bestAnswer.getQuickDescription();

        if(!hasAction) {
            layAction.setVisibility(View.GONE);

            if(!TextUtils.isEmpty(quickDescription))
                mBinding.tvAnswer.setText(quickDescription);
            else
                mBinding.layAnswer.setVisibility(View.GONE);
        } else {

            final String actionStr = item.getActionStr();
            if(actionStr.startsWith("关注") || actionStr.startsWith("提")) {
                mBinding.tvAnswer.setText(item.getBestAnswer().getQuickDescription());
            } else {
                mBinding.tvAnswer.setText(item.getQuickDescription());
            }

            mBinding.tvActionTop.setText(item.getActionStr());

            mBinding.tvActionDesc.setText(quickDescription);

            final PbBean.PbUserSimple userInfo = bestAnswer.getUserInfo();
            mBinding.tvActionName.setText(userInfo.getNickname());

            final ImageView ivTop = mBinding.iconAction;
            final ViewGroup.LayoutParams lpHeader = ivTop.getLayoutParams();
            lpHeader.width = lpHeader.height = DisplayUtils.getDeviceWidth(context) / 8;
            ImageLoader.getInstance().displayImage(userInfo.getAvatar().getUrl(), ivTop);

            layAction.setOnClickListener(this);
        }
    }


    @Override
    public void onClick(View v) {

        switch(v.getId()) {

            case R.id.layTitle:
                QuizDetailActivity.start(context, item.getQuestionId(), isAuthor());
                break;

            case R.id.layAnswer:
//                ReplyDetaiActivtiy.start(v.getContext(), item.getBestAnswer().getAnswerId(), isAuthor(), item.getVoteOptionList().size());
                break;

            case R.id.layAction:
            case R.id.vUserInfo:
                final PbBean.PbUserSimple userInfo = item.getUserInfo();
                UserInfoActivity.start(context, userInfo.getUserId(), userInfo.getIsDesigner());
                break;

        }
    }

    private boolean isAuthor() {return TextUtils.equals(item.getUserInfo().getUserId(), ConfigUtils.getUserId(context));}

}
